<?php
class model_lhbase_adminnode extends gdl_components_basemodel 
{

    public function primarykey() {
        return 'id';
    }

    public function tableName() {
        return 'lh_admin_node';
    }
	
	
	//权限列表
	 public function getMenu($admin='gl',$uid,$role_id=0)
    {
		
        if ($admin=='zg') {
            $nodes = $this->select("status=1 AND moduleId > 0","id,pid,name,moduleId,title,type")->items;
        } else {
			/**个人权限***/
			
			$adminuseraccess = new model_lhbase_adminuseraccess();
			$adminroleuser = new model_lhbase_adminroleuser();
			if(!$role_id){
				$role = $adminroleuser->selectOne(['userId'=>$uid],'roleId');
				$role_id = empty($role['roleId']) ? 0 : $role['roleId'];
			}
			$node_ids = $adminuseraccess->select(['roleId'=>$role_id,'userId'=>$uid], '*')->items;

			/**个人权限 end***/
			/*$adminroleuser = new model_newlh_adminroleuser();
			$node_ids = $$adminroleuser->select(['role_id'=>$role_id,'user_id'=>$uid], 'node_id')->items;*/
			$condition = "(user.userId='{$uid}'";
			
			$leftjoin = array(
				'lh_admin_access AS access' => 'access.nodeId='.$this->tableName().'.id ',
                'lh_admin_role AS role' => 'access.roleId=role.id',
                'lh_admin_role_user AS user' => 'user.roleId=role.id ',
                
            );
			$condition .= " and role.status=1 and role.id='{$role_id}' and ".$this->tableName().".status=1 and ".$this->tableName().".moduleId > 0";
			$condition.=')';
			if (!empty($node_ids)){
				$node_ids = array_column($node_ids,'nodeId');
				$condition.=' or ('.$this->tableName().'.id in('.implode(',',$node_ids).') and role.status=1 and '.$this->tableName().'.status=1)';
			}
			
		
			
			
			$nodes = $this->select($condition,$this->tableName().'.id,'.$this->tableName().'.name,'.$this->tableName().'.pid,'.$this->tableName().'.moduleId,'.$this->tableName().'.title,'.$this->tableName().'.type','ORDER BY '.$this->tableName().'.sort ASC','',$leftjoin)->items;
        }

        return $nodes;
    }
	
	
}
